package com.sykj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bean.Search;
import com.sykj.bean.bis.BisNurseBag;
import com.sykj.bean.bis.BisNurseProject;
import com.sykj.mapper.GameLoadMapper;
import com.sykj.service.SearchService;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: BeautyClub
 * @description:
 * @author: qiaoli
 * @create: 2020-08-14 10:45
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    GameLoadMapper gameLoadMapper;
    @Autowired
    SearchService searchService;
     @RequestMapping("/queryByid/{customUserId}")
    public ResultBody queryByuserid(@PathVariable("customUserId")String customUserId){
         List<BisNurseProject> bisNurseProjects = gameLoadMapper.querySearchByuserid(customUserId);
         return new ResultBody(CommonEnum.SUCCESS,bisNurseProjects);
    }

  @RequestMapping("/lisi/{projectId}/{customUserId}")
    public ResultBody savaLisi(@PathVariable("projectId")String projectId,@PathVariable("customUserId")String customUserId){
          QueryWrapper<Search> queryWrapper=new QueryWrapper<>();
          queryWrapper.eq("project_id",projectId);
          queryWrapper.eq("user_id",customUserId);
        Search one = searchService.getOne(queryWrapper);
        if(one!=null){
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("project_id",projectId);
            String  oldnum=one.getSNum();
            int newnum=Integer.parseInt(oldnum)+1;
            updateWrapper.set("s_num",newnum);
            boolean update = searchService.update(updateWrapper);
            if(update==true){
                return new ResultBody(CommonEnum.SUCCESS);
            }
        }else{
            Search search=new Search();
            search.setSType("1");
            search.setUserId(customUserId);
            search.setProjectId(projectId);
            search.setSNum("1");
            boolean save = searchService.save(search);
               if(save==true){
                 return   new ResultBody(CommonEnum.SUCCESS);
               }
        }


         return  new ResultBody(CommonEnum.NOT_FOUND);
    }
/**
 * 功能描述:  删除用户的查询历史记录
 * @Param: [customUserId]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/14 20:26
 */

    @RequestMapping("/deleteAll/{customUserId}")
    public   ResultBody   deleteAllbyuser(@PathVariable("customUserId")String customUserId){
        UpdateWrapper<Search> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("user_id",customUserId);

        boolean b = searchService.remove(updateWrapper);

        if(b){
            return  new ResultBody(CommonEnum.SUCCESS);
        }
       return  new ResultBody(CommonEnum.WeChat_QUERY_ERROR);
    }
}
